{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/llm/neutrino.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Neutrino AI\n",
    "\n",
    "Neutrino lets you intelligently route queries to the best-suited LLM for the prompt, maximizing performance while optimizing for costs and latency.\n",
    "\n",
    "Check us out at: <a href=\"https://www.neutrinoapp.com/\">neutrinoapp.com</a>\n",
    "Docs: <a href=\"https://docs.neutrinoapp.com/\">docs.neutrinoapp.com</a>\n",
    "Create an API key: <a href=\"https://platform.neutrinoapp.com/\">platform.neutrinoapp.com</a>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-llms-neutrino"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-index"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Set Neutrino API Key env variable\n",
    "You can create an API key at: <a href=\"https://platform.neutrinoapp.com/\">platform.neutrinoapp.com</a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"NEUTRINO_API_KEY\"] = \"<your-neutrino-api-key>\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Using Your Router\n",
    "A router is a collection of LLMs that you can route queries to. You can create a router in the Neutrino <a href=\"https://platform.neutrinoapp.com/\">dashboard</a> or use the default router, which includes all supported models.\n",
    "You can treat a router as a LLM.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimal model: gpt-3.5-turbo\n",
      "a subatomic particle that is electrically neutral and has a very small mass. It is one of the fundamental particles that make up the universe. Neutrinos are extremely difficult to detect because they interact very weakly with matter, making them able to pass through most materials without any interaction. They are produced in various natural processes, such as nuclear reactions in the Sun and other stars, as well as in particle interactions on Earth. Neutrinos have played a significant role in advancing our understanding of particle physics and the universe.\n"
     ]
    }
   ],
   "source": [
    "from llama_index.llms.neutrino import Neutrino\n",
    "from llama_index.core.llms import ChatMessage\n",
    "\n",
    "llm = Neutrino(\n",
    "    # api_key=\"<your-neutrino-api-key>\",\n",
    "    # router=\"<your-router-id>\"  # (or 'default')\n",
    ")\n",
    "\n",
    "response = llm.complete(\"In short, a Neutrino is\")\n",
    "print(f\"Optimal model: {response.raw['model']}\")\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimal model: mistralai/Mixtral-8x7B-Instruct-v0.1\n",
      "assistant:  Statically typed languages and dynamically typed languages are categories of programming languages based on how they handle variable types.\n",
      "\n",
      "In statically typed languages, the type of a variable is determined at compile-time, which means that the type is checked before the program is run. This ensures that variables are always used in a type-safe manner, preventing many types of errors from occurring at runtime. Examples of statically typed languages include Java, C, C++, and C#.\n",
      "\n",
      "In dynamically typed languages, the type of a variable is determined at runtime, which means that the type is checked as the program is running. This provides more flexibility, as variables can be assigned values of different types at different times, but it also means that type errors may not be caught until the program is running, which can make debugging more difficult. Examples of dynamically typed languages include Python, Ruby, JavaScript, and PHP.\n",
      "\n",
      "One key difference between statically typed and dynamically typed languages is that statically typed languages tend to be more verbose, requiring explicit type declarations for variables, while dynamically typed languages are more concise and allow for implicit type declarations. However, this also means that statically typed languages can catch type errors earlier in the\n"
     ]
    }
   ],
   "source": [
    "message = ChatMessage(\n",
    "    role=\"user\",\n",
    "    content=\"Explain the difference between statically typed and dynamically typed languages.\",\n",
    ")\n",
    "resp = llm.chat([message])\n",
    "print(f\"Optimal model: {resp.raw['model']}\")\n",
    "print(resp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Streaming Responses"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimal model: anthropic.claude-instant-v1\n",
      " According to the latest UN estimates, the population of Mexico is approximately 128 million as of 2020. Mexico has the 10th largest population in the world."
     ]
    }
   ],
   "source": [
    "message = ChatMessage(\n",
    "    role=\"user\", content=\"What is the approximate population of Mexico?\"\n",
    ")\n",
    "resp = llm.stream_chat([message])\n",
    "for i, r in enumerate(resp):\n",
    "    if i == 0:\n",
    "        print(f\"Optimal model: {r.raw['model']}\")\n",
    "    print(r.delta, end=\"\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
